package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.*;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealdishMapper setmealdishMapper;

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Transactional
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //已经用联表查询实现
        /*List<DishVO> dishList = dishMapper.getAll(dishPageQueryDTO);
        dishList.forEach(dish->{
            String nameByCategoryId = categoryMapper.getNameByCategoryId(dish.getCategoryId());
            dish.setCategoryName(nameByCategoryId);
        });
        Page page = (Page) dishList;*/

        Page<DishVO> page = dishMapper.getAll(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Transactional
    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        //获得菜品id
        Long dishId = dish.getId();
        System.out.println(dishId);
        //获取菜品的口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
            dishFlavorMapper.insertFlavorList(flavors);
        }
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        /*//根据分类id获取分类名称
        String name = categoryMapper.getNameByCategoryId(dishVO.getCategoryId());*/
        //根据口味id获取口味的值  已用resultMap实现
        /*List<DishFlavor> flavors = dishFlavorMapper.getFlavorById(id);
        //dishVO.setCategoryName(name);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(flavors);*/

        return dishMapper.selectById(id);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> getByCategoryId(Dish dish) {
        return dishMapper.selectByCategoryId(dish);
    }

    /**
     * 菜品起售或停售
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder().
                id(id).
                status(status).
                build();
        dishMapper.update(dish);

        List<Setmeal> setmeals = setmealMapper.selectByDishId(id);
        log.info("套餐id:{}",setmeals);
        if (setmeals.size() > 0 && status.equals(StatusConstant.DISABLE)){
            setmeals.forEach(setmeal -> {
                //log.info("setmeal:{}",setmeal);
                setmeal.setStatus(StatusConstant.DISABLE);
                setmealMapper.update(setmeal);
            });
        }
}

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.update(dish);

        Long dishId = dish.getId();
        dishFlavorMapper.deleteByDishIdS(dishId);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
            dishFlavorMapper.insertFlavorList(flavors);
        }
    }

    /**
     * 批量修改菜品
     *
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        ids.forEach(id -> {
            DishVO dish = dishMapper.selectById(id);
            //判断当前要删除的菜品状态是否为起售中
            if (dish.getStatus().equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            //判断当前要删除的菜品是否被套餐关联了
            List<Dish> setmeal = setmealdishMapper.selectByDishId(id);

            if (setmeal != null && setmeal.size() > 0) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        });
        //根据id批量删除菜品
        dishMapper.delete(ids);
        //根据菜品id批量删除口味
        dishFlavorMapper.deleteByDishId(ids);
    }
}
